package com.rams.controller;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rams.domain.vo.User;
import com.rams.domain.vo.UserCircle;
import com.rams.service.LoginService;
import com.rams.service.UserService;
import com.rams.service.vo.DisplayVO;
 
/**
 * @author Swapnil Desai
 *
 */
@Controller
public class LoginController {
	
	@Autowired
	LoginService loginService;
	
	@Autowired
	UserService userService;
	
    /**
     * This method is used to validate user.
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView loginUser(User user, HttpServletRequest request) {
    	System.out.println("RAMSController.loginUser()");
        DisplayVO displayVO = loginService.validateLogin(user);
       
        ModelAndView modelAndView = null;
        if(null != displayVO && displayVO.isStatus()){
        	modelAndView = new ModelAndView("hello");
        	HttpSession session = request.getSession(true);
            session.setAttribute("userId", displayVO.getUser().getUsername());
        } else {
        	modelAndView = new ModelAndView("login");
        }
        
        modelAndView.addObject("displayVO", displayVO);
        return modelAndView;
    }
    
    /**
     * This method is used to create new user.
     * @param newUser
     * @param request
     * @return
     */
    @RequestMapping(value = "/createnewuser", method = {RequestMethod.POST, RequestMethod.GET })
    public ModelAndView createUser(User newUser, HttpServletRequest request) {
 
    	System.out.println("RAMSController.createUser()");
    	if(null != newUser){
    		loginService.createNewUser(newUser);
    	}
    	ModelAndView model = new ModelAndView("login");
    	DisplayVO displayVO = new DisplayVO();
    	displayVO.setStatus(true);
    	displayVO.setUser(newUser);
    	
    	model.addObject("user", "New User");
    	model.addObject("displayVO", displayVO);
    	
        return model;
    }
    
    
    /**
     * This method is used to create new circle and add it to currently logged in user circles's.
     * @param newUserCircle
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/createnewcircle", method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView createNewUserCircle(UserCircle newUserCircle, HttpServletRequest request, HttpSession session) {
 
    	System.out.println("RAMSController.createUser()");
    	String userName = "";
    	if(null != session.getAttribute("userId") && !session.getAttribute("userId").equals("")){
    		userName = (String)session.getAttribute("userId");
    	}
    	DisplayVO displayVO = userService.addNewCircle(newUserCircle, userName);
    	ModelAndView model = new ModelAndView("hello");
    	model.addObject("displayVO", displayVO);
    	
        return model;
    }
    
    @RequestMapping(value = "/logout", method = {RequestMethod.POST,RequestMethod.GET})
    public ModelAndView logoutUser(HttpServletRequest request, HttpSession session) {
 
    	System.out.println("RAMSController.logoutUser()");
    	session.removeAttribute("userId");
    	ModelAndView model = new ModelAndView("login");
//    	model.addObject("displayVO", "");
    	
        return model;
    }
    
    
    
}